package com.test;

import java.io.IOException;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.first.wchat.R;
import com.wchat.network.Actions;
import com.wchat.network.Configs;
import com.wchat.network.Controller;
import com.wchat.network.device.Device;
import com.wchat.network.device.Group;
import com.wchat.network.device.GroupParam;
import com.wchat.network.message.Message;
import com.wchat.network.message.TextMessage;
import com.wchat.util.UserSettings;

public class WcActivity extends Activity {

	BroadcastReceiver receiver = new BroadcastReceiver() {

		@Override
		public void onReceive(Context context, Intent intent) {
			String action = intent.getAction();
			if (Actions.ACTION_RECEIVE_MESSAGE.equals(action)) {
				Message message = (Message) intent.getSerializableExtra("message");
				Configs.i("activity get message: " + message);
				Controller.getInstance().handle(message);
			} else if (Actions.ACTION_DEVICE_FOUND.equals(action)) {
				Device device = (Device) intent.getSerializableExtra("device");
				Configs.i("device found: " + device);
				TextMessage message = Message.createTextMessage("hello, " + device.getName(), Message.GROUP_ONE2ONE);
				try {
					device.sendMessage(message);
//					BroadCastTarget.getInstance().sendMessage(message);
				} catch (IOException e) {
					e.printStackTrace();
				}
				
				GroupParam param = new GroupParam();
				param.name = "Group by " +UserSettings.getSettingString(WcActivity.this, UserSettings.Keys.USER_NAME);
				Controller.getInstance().createGroup(param);
				
			} else if (Actions.ACTION_DEVICE_UPDATED.equals(action)) {
				Device device = (Device) intent.getSerializableExtra("device");
				Configs.i("device update: " + device);
			} else if (Actions.ACTION_DEVICE_LOST.equals(action)) {
				Device device = (Device) intent.getSerializableExtra("device");
				Configs.i("device lost: " + device);
			} else if (Actions.ACTION_GROUP_FOUND.equals(action)) {
				Group group = (Group) intent.getSerializableExtra("group");
				Configs.i("group found: " + group);
				Toast.makeText(WcActivity.this, group.toString(), Toast.LENGTH_SHORT).show();
			}
		}
	};

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		String name = UserSettings.getSettingString(this, UserSettings.Keys.USER_NAME);
		if (name == null || "".equals(name)) {
			showInputName();
		} else {
			init();
		}

		IntentFilter filter = new IntentFilter();
		filter.addAction(Actions.ACTION_RECEIVE_MESSAGE);
		filter.addAction(Actions.ACTION_DEVICE_FOUND);
		filter.addAction(Actions.ACTION_DEVICE_UPDATED);
		filter.addAction(Actions.ACTION_DEVICE_LOST);
		filter.addAction(Actions.ACTION_GROUP_FOUND);
		registerReceiver(receiver, filter);
	}

	private void init() {
		Controller.getInstance().start(this);
	}

	private void showInputName() {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		View view = View.inflate(this, R.layout.promote_name, null);
		final EditText namEditText = (EditText) view.findViewById(R.id.input_name);
		builder.setView(view);
		builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				String name = namEditText.getEditableText().toString();
				UserSettings.putSettingString(WcActivity.this, UserSettings.Keys.USER_NAME, name);
				UserSettings.putSettingString(WcActivity.this, UserSettings.Keys.ID, generateUniqeId());
				init();
			}
		});
		builder.setNegativeButton("Cancel", null);
		builder.show();
	}

	private static String generateUniqeId() {
		return System.currentTimeMillis() + "";
	}

	@Override
	protected void onDestroy() {
		unregisterReceiver(receiver);
		Controller.getInstance().stop();
		super.onDestroy();
	}
}